package com.yinbw.interview;

import com.yinbw.interview.controller.QuestionsAction;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 在test文件下不能使用Scanner的原因主要是因为IDEA的测试环境（如JUnit测试）默认不支持从控制台读取输入
 * Help - Edit Custom VM Options - 在打开的idea.vmoptions文件中，在末尾添加以下代码：-Deditable.java.test.console=true
 * 保存并重启IDEA，这样设置后，Scanner就可以在测试中接收键盘输入了‌
 */

@RunWith(SpringRunner.class)
@SpringBootTest
public class Questions {

    public static final boolean flag = true;
    //    public static final boolean flag = false;
    public static final String[] q1 = {"SQL", "索引", "事务", "JavaSE", "String", "数组", "集合", "面向对象", "IO流"};
    public static final String[] q2 = {"反射", "线程", "Spring", "SpringMVC", "SpringBoot", "SpringCloud"};
    public static final String[] q3 = {"MyBatisPlus", "Redis", "设计模式", "Sentinel", "RocketMQ"};

    @Autowired
    private QuestionsAction questionsAction;

    @Test
    public void test01() throws Exception {

        List<com.yinbw.interview.entity.Questions> questions = questionsAction.questionsList();
        Collections.shuffle(questions);
        Set<String> types = questions.stream()
                .map(com.yinbw.interview.entity.Questions::getType) // 提取name属性
                .collect(Collectors.toSet()); // 收集到Set中
        List<String> types2 = new ArrayList<>(types);
        Collections.shuffle(types2);
        Scanner scanner = new Scanner(System.in); // 创建Scanner对象
        if (flag == false) {
            for (String type : types2) {
                System.out.println("===========" + type + "==============");
                for (com.yinbw.interview.entity.Questions item : questions) {
                    if (type.equals(item.getType())) {
                        scanner.nextLine(); // 读取字符串
                        System.out.println("-----------------");
                        System.out.println("  " + item.getContent());
                    }
                }
            }
        } else {
            System.out.println("请选择复习的小组 1 或 2 或 3");
            int i1 = scanner.nextInt();
            if (1 == i1) {
                for (int i = 0; i < q1.length; i++) {
                    System.out.println("===========" + q1[i] + "==============");
                    for (com.yinbw.interview.entity.Questions item : questions) {
                        if (q1[i].equals(item.getType())) {
                            scanner.nextLine(); // 读取字符串
                            System.out.println("-----------------");
                            System.out.println("  " + item.getContent());
                        }
                    }
                }
            } else if (2 == i1) {
                for (int i = 0; i < q2.length; i++) {
                    System.out.println("===========" + q2[i] + "==============");
                    for (com.yinbw.interview.entity.Questions item : questions) {
                        if (q2[i].equals(item.getType())) {
                            scanner.nextLine(); // 读取字符串
                            System.out.println("-----------------");
                            System.out.println("  " + item.getContent());
                        }
                    }
                }
            } else {
                for (int i = 0; i < q3.length; i++) {
                    System.out.println("===========" + q3[i] + "==============");
                    for (com.yinbw.interview.entity.Questions item : questions) {
                        if (q3[i].equals(item.getType())) {
                            scanner.nextLine(); // 读取字符串
                            System.out.println("-----------------");
                            System.out.println("  " + item.getContent());
                        }
                    }
                }
            }
        }
        scanner.close(); // 关闭Scanner对象（可选）
        Thread.sleep(15000);
    }
}
